<template>
  <div v-loading="loading" class="app-container">
    <div class="search-box">
      <div class="search-form">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item prop="name">
            <el-input v-model="searchForm.name" maxlength="250" placeholder="成果名称" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="type">
            <el-select v-model="searchForm.type" placeholder="成果形式" clearable style="width:140px;">
              <el-option
                v-for="item in typeOptions"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="status">
            <el-select v-model="searchForm.status" placeholder="状态" clearable style="width:140px;">
              <el-option
                v-for="item in statusOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发表日期" prop="pressDateBegin">
            <el-date-picker v-model="searchForm.pressDateBegin" type="date" style="width:140px;" />
          </el-form-item>
          <el-form-item label="-" prop="pressDateEnd">
            <el-date-picker v-model="searchForm.pressDateEnd" type="date" style="width:140px;" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button icon="el-icon-remove-outline" title="重置" @click="resetSearchForm" />
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar-box clearfix">
        <el-button v-loading="downloadLoading" type="success" icon="el-icon-download" @click="expexcel">导出</el-button>
        <el-popover
          width="300"
          trigger="click"
          style="float: right;"
        >
          <div class="filter-columns">
            <el-checkbox-group v-model="selectedColumns">
              <el-checkbox label="content">成果描述</el-checkbox>
              <el-checkbox label="firstAuthor">第一作者</el-checkbox>
              <el-checkbox label="firsAuthorOrg">第一作者所属单位</el-checkbox>
              <el-checkbox label="publishLevel">刊物级别</el-checkbox>
              <el-checkbox label="publishNo">期刊号/书号</el-checkbox>
              <el-checkbox label="press">出版单位</el-checkbox>
            </el-checkbox-group>
          </div>
          <el-button slot="reference">更多列... <i class="el-icon-arrow-down" /></el-button>
        </el-popover>
      </div>
    </div>

    <el-table
      ref="multipleTable"
      stripe
      :data="tableData"
      border
      class="table"
    >
      <el-table-column prop="applyName" label="申请标题" />
      <el-table-column prop="applicantName" label="申请人" />
      <el-table-column prop="deptName" label="所属部门" />
      <el-table-column prop="applyTime" label="申请日期" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.applyTime | parseTime("{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="成果名称" />
      <el-table-column prop="typeName" label="成果形式" />
      <el-table-column v-if="showColumn('content')" prop="content" label="成果描述" />
      <el-table-column prop="role" label="承担角色" />
      <el-table-column v-if="showColumn('firstAuthor')" prop="firstAuthor" label="第一作者" />
      <el-table-column v-if="showColumn('firsAuthorOrg')" prop="firsAuthorOrg" label="第一作者所属单位" />
      <el-table-column prop="publishName" label="刊物名称" />
      <el-table-column v-if="showColumn('publishLevel')" prop="publishLevelName" label="刊物级别" />
      <el-table-column v-if="showColumn('publishNo')" prop="publishNo" label="期刊号/书号" />
      <el-table-column v-if="showColumn('press')" prop="press" label="出版单位" />
      <el-table-column prop="pressDate" label="发表日期" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.pressDate | parseTime("{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="approveName" label="审核人" />
      <el-table-column prop="approveTime" label="审核日期" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.approveTime | parseTime("{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="auditComment" label="审核意见" />
      <el-table-column prop="statusName" label="状态" />
      <el-table-column label="操作" width="70" align="left">
        <template slot-scope="scope">
          <el-button type="text" @click="handleShow(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="curPage" :limit.sync="limit" @pagination="getList" />
    <!--表单组件-->
    <sForm ref="sform" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'
import { downloadFile } from '@/utils'
import sForm from './show'
export default {
  name: 'PublishList',
  components: { Pagination, sForm },
  data() {
    return {
      curPage: 1,
      limit: 5,
      total: 0,
      loading: true,
      downloadLoading: false,
      selectedColumns: [],
      orderByClause: null,
      typeOptions: [],
      statusOptions: [{ id: 1, name: '等待审批' }, { id: 2, name: '等待审核' }, { id: 3, name: '不通过,重新申请' }, { id: 10, name: '审核通过' }, { id: 11, name: '审核不通过' }],
      tableData: [],
      searchForm: {
        name: '',
        type: '',
        pressDateBegin: '',
        pressDateEnd: '',
        status: ''
      }
    }
  },
  created() {
    this.getTypeOptions()
    this.getList()
  },
  methods: {
    getTypeOptions() {
      request({ url: '/sys/dirtionary/getDirtionaryItems', params: { dirtionary: 'PUBLISHTYPE' }, method: 'get' })
        .then(data => {
          this.typeOptions = data
        })
    },
    showColumn(column) {
      for (let j = 0, len = this.selectedColumns.length; j < len; j++) {
        if (column === this.selectedColumns[j]) {
          return true
        }
      }
      return false
    },
    // 获取数据
    getList() {
      this.loading = true
      const param = {}
      const queryPage = {}
      queryPage.limit = this.limit
      queryPage.offset = this.limit * (this.curPage - 1)
      param.page = queryPage
      if (this.orderByClause && this.orderByClause.trim() !== '') {
        param.orderByClause = this.orderByClause
      }
      this.copyNotBlankField(this.searchForm, param)
      request({ url: '/edu/publish/findPage', method: 'post', data: param })
        .then(res => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
          this.curPage = res.pageNumber
        })
        .catch(e => {
          this.loading = false
          this.$message.error('查询出错')
        })
    },
    onSearch() {
      this.getList()
    },
    resetSearchForm() {
      this.$refs['searchForm'].resetFields()
    },
    handleShow(id) {
      this.$refs.sform.show(id)
    },
    expexcel() {
      const param = {}
      this.copyNotBlankField(this.searchForm, param)
      if (this.orderByClause && this.orderByClause.trim() !== '') {
        param.orderByClause = this.orderByClause
      }
      this.downloadLoading = true
      request({ url: '/edu/publish/export', method: 'post', data: param, responseType: 'blob' })
        .then(data => {
          downloadFile(data, '教师发表', 'xlsx')
          this.downloadLoading = false
        })
        .catch(e => {
          this.downloadLoading = false
          this.$message.error('导出出错')
        })
    }
  }
}
</script>
